package com.conan.zone.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.type.TypeReference;

/**
 * 
 * @author Conan
 * 
 */
public class JsonTool {
	private static final ObjectMapper mapper = new ObjectMapper();

	public ObjectMapper getMapper() {
		return mapper;
	}

	static {
		mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}

	/**
	 * 
	 * @param obj
	 * @return
	 */
	public static String writeValueAsString(Object obj) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @param paramString
	 * @param paramClass
	 * @return
	 */
	public static <T> T readValue(String paramString, Class<T> paramClass) {
		try {
			return mapper.readValue(paramString, paramClass);
		} catch (JsonParseException localJsonParseException) {
			localJsonParseException.printStackTrace();
			throw new IllegalArgumentException(
					localJsonParseException.getMessage());
		} catch (JsonMappingException localJsonMappingException) {
			localJsonMappingException.printStackTrace();
			throw new IllegalArgumentException(
					localJsonMappingException.getMessage());
		} catch (IOException localIOException) {
			localIOException.printStackTrace();
			throw new IllegalArgumentException(localIOException.getMessage());
		}
	}

	/**
	 * 
	 * @param paramString
	 * @return
	 */
	public static ArrayList<Map<String, Object>> readValue2List(
			String paramString) {
		ArrayList localArrayList = null;
		try {
			localArrayList = (ArrayList) mapper.readValue(paramString,
					TypeFactory.collectionType(ArrayList.class, Map.class));
		} catch (JsonParseException localJsonParseException) {
			localJsonParseException.printStackTrace();
			throw new IllegalArgumentException(
					localJsonParseException.getMessage());
		} catch (JsonMappingException localJsonMappingException) {
			localJsonMappingException.printStackTrace();
			throw new IllegalArgumentException(
					localJsonMappingException.getMessage());
		} catch (IOException localIOException) {
			localIOException.printStackTrace();
			throw new IllegalArgumentException(localIOException.getMessage());
		}
		return localArrayList;
	}

	/**
	 * 
	 * @param paramString
	 * @param paramClass
	 * @return
	 */
	public static <T> ArrayList<T> readValue2List(String paramString,
			Class<T> paramClass) {
		try {
			return ((ArrayList) mapper.readValue(paramString,
					TypeFactory.collectionType(ArrayList.class, paramClass)));
		} catch (JsonParseException localJsonParseException) {
			localJsonParseException.printStackTrace();
			throw new IllegalArgumentException(
					localJsonParseException.getMessage());
		} catch (JsonMappingException localJsonMappingException) {
			localJsonMappingException.printStackTrace();
			throw new IllegalArgumentException(
					localJsonMappingException.getMessage());
		} catch (IOException localIOException) {
			localIOException.printStackTrace();
			throw new IllegalArgumentException(localIOException.getMessage());
		}
	}

	/**
	 * 
	 * @param paramString
	 * @param paramTypeReference
	 * @return
	 */
	public static <T> ArrayList<T> readValue2List(String paramString,
			TypeReference<List<T>> paramTypeReference) {
		try {
			return ((ArrayList) mapper.readValue(paramString,
					paramTypeReference));
		} catch (JsonParseException localJsonParseException) {
			localJsonParseException.printStackTrace();
			throw new IllegalArgumentException(
					localJsonParseException.getMessage());
		} catch (JsonMappingException localJsonMappingException) {
			localJsonMappingException.printStackTrace();
			throw new IllegalArgumentException(
					localJsonMappingException.getMessage());
		} catch (IOException localIOException) {
			localIOException.printStackTrace();
			throw new IllegalArgumentException(localIOException.getMessage());
		}
	}

}
